﻿using CRM.Api.Common;
using CRM.Api.Models;
using CRM.Infrastructure.Dto;
using CRM.Infrastructure.Entity;
using CRM.Infrastructure.Models;
using CRM.Infrastructure.Service;
using CRM.Utility;
using Dapper.Extensions;
using System;
using System.Collections.Generic;
using System.Web.Http;

namespace CRM.Api.Controllers
{
    /// <summary>
    /// 代办
    /// </summary>
    public class TodoController : BaseApiController
    {
        TodoService tService = new TodoService();
        CluesService cService = new CluesService();

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<Page<TodoDto>> GetPages(TodoSearchModel model)
        {
            var page = tService.GetPages(model);
            foreach (var item in page.rows)
            {
                if (string.IsNullOrWhiteSpace(item.UserName))
                {
                    string first = "";
                    if (!string.IsNullOrWhiteSpace(item.HisUserName))
                    {
                        first = item.HisUserName.Substring(0, 1);
                    }

                    switch (item.Sex)
                    {
                        case 1:
                            item.UserName = first + " 先生";
                            break;
                        case 2:
                            item.UserName = first + " 女士";
                            break;
                        default:
                            item.UserName = first + " 未知";
                            break;
                    }
                }
                item.HisUserName = "";
            }
            return ResultData<Page<TodoDto>>.Success(page);
        }

        /// <summary>
        /// 添加代办
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<int> AddTodo(TodoModel model)
        {
            object id;

            if (model.IsNewTodo > 0)
            {
                Todo todo = new Todo
                {
                    CreateDate = DateTime.Now,
                    IsActive = 1,
                    IsRead = 0,
                    Comment = model.Comment,
                    ClueID = model.ClueID,
                    FlowStep = model.FlowStep,
                    PreTodoID = model.PreTodoID,
                    TodoDate = model.TodoDate,
                    UserID = model.UserID,
                    PurchaseStep = model.PurchaseStep
                };
                todo.TodoDateFrt = todo.TodoDate.ToString("yyyyMMdd");
                id = tService.Insert(todo);
            }
            else
            {
                id = 1;
            }
           

            //设置代办已读
            if (model.PreTodoID > 0)
            {
                tService.UpdateRead(model.PreTodoID);
            }

            //更新线索状态
            var clue = cService.SingleOrDefault(model.ClueID);
            if (clue != null)
            {
                clue.PurchaseStep = model.PurchaseStep;
                clue.FlowStep = model.FlowStep;
                clue.UpdateDate = DateTime.Now;
                cService.Update(clue);
            }

            return ResultData<int>.Success(EConvert.ToInt(id));
        }

        /// <summary>
        /// 获取统计
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<StatisticModel> GetStatistic(long userId)
        {
            List<int> lst = tService.GetStatistic(userId, DateTime.Now);
            return ResultData<StatisticModel>.Success(new StatisticModel()
            {
                Total = lst[0],
                UnRead = lst[1]
            });
        }
    }
}
